/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dungeonsolver.helper;

import java.awt.Point;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.Buffer;
import java.util.Vector;

/**
 *
 * @author user
 */
public final class Utility {
	private Utility() {
		//Create logs
		m_Logs = new Vector<String>();
	}

	public static synchronized Utility instance() {
		//Instantiate
		if (m_Instance == null) m_Instance = new Utility();
		return m_Instance;
	}

	@Override
	public Object clone() {	return  null;	}

	public void addLog(String log) {
		//Add
		m_Logs.add(log);
	}

	public void flushLog() {
		//Write each line on the logs
		for (int i = 0; i < m_Logs.size(); i++)
			System.out.println(m_Logs.elementAt(i));

		//Create logs
		m_Logs = new Vector<String>();
	}

	public void flushLog(String file) {
		//Skip if empty
		if (m_Logs.isEmpty()) return;

		try {
			//Create writers
			FileWriter File			= new FileWriter(file);
			BufferedWriter Writer	= new BufferedWriter(File);

			//Write each line on the logs
			for (int i = 0; i < m_Logs.size(); i++) {
				Writer.write(m_Logs.elementAt(i));
				Writer.newLine();
			}

			//Close file
			Writer.close();
		} catch (Exception e) {}

		//Create logs
		m_Logs = new Vector<String>();
	}

	public boolean isPointACloserThanB(Point source, Point a, Point b) {
		//Check
		if (source == null || a == null || b == null) return false;

		//Calculate
		double DistanceA = a.distanceSq(source.x, source.y);
		double DistanceB = b.distanceSq(source.x, source.y);

		return DistanceA < DistanceB;
	}

	public int calculateManhattanRange(Point a, Point b) {
		//Calculate
		return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
	}

	//The only instance
	private static Utility m_Instance = null;

	//Member
	private Vector<String> m_Logs;
}
